package com.banch.cloud.order.controller;

import com.banch.cloud.common.entity.Payment;
import com.banch.cloud.common.vo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * order controller
 *
 * @author banch
 * @version 1.0
 * @date 2020/5/5
 */
@Slf4j
@RestController
@RequestMapping(value = {"/consumer/order/"})
public class OrderController {

    /**
     * restTemplate
     */
    @Resource
    private RestTemplate restTemplate;

    /**
     * PAYMENT URL
     */
    //public static final String PAYMENT_URL = "http://localhost:8001";
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    /**
     * create
     *
     * @param payment Payment entity
     * @return
     * @author banch
     * @date 2020/5/5
     * @throw
     **/
    @PostMapping(value = {"/create"})
    public CommonResult create(Payment payment) {

        log.info("Request http server.payment:{}", payment);
        CommonResult commonResult = restTemplate.postForObject(PAYMENT_URL + "/payment/create",
                payment, CommonResult.class);
        log.info("Request http server end.result:{}", commonResult);

        return commonResult;
    }

    /**
     * get by id
     *
     * @param id id
     * @return
     * @author banch
     * @date 2020/5/5
     * @throw
     **/
    @GetMapping(value = {"/get/{id}"})
    public CommonResult getById(@PathVariable("id") Long id) {

        log.info("Request http server.id:{}", id);
        CommonResult result = restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
        log.info("Request http server end.result:{}", result);

        return result;
    }


    /**
     * zipkin+sleuth
     *
     * @author banch
     * @date 2020/7/24
     * @param
     * @return
     * @throw
     **/
    @GetMapping("/payment/zipkin")
    public String paymentZipkin() {
        String result = restTemplate.getForObject("http://localhost:8001" + "/payment/zipkin/", String.class);
        return result;
    }

}